/***
 * @Description: 本文件主要定义的是socket编程所要实现的函数和数据结构的头文件
 * @Author: Liverpool
 * @Date: 2024-09-08 23:14:34
 * @LastEditTime: 2024-09-08 23:15:29
 * @LastEditors: Liverpool
 */

#ifndef SOCKET_H
#define SOCKET_H

#include "stdint.h"
#include "ipv4.h"

#undef INADDR_ANY
#define INADDR_ANY (ULONG)0x00000000

#undef AF_INET
#define AF_INET 2

#undef SOCK_RAW
#define SOCK_RAW 0

#undef IPPROTO_ICMP
#define IPPROTO_ICMP 1

#undef SOL_SOCKET
#define SOL_SOCKET 0

#undef SO_RCVTIMEO
#define SO_RCVTIMEO 1

#undef SO_SNDTIMEO
#define SO_SNDTIMEO 2

typedef int x_socklen_t;

struct x_timeval
{
    int tv_sec;
    int tv_usec;
};

struct x_in_addr
{
    union
    {
        struct
        {
            uint8_t addr0;
            uint8_t addr1;
            uint8_t addr2;
            uint8_t addr3;
        };
        uint8_t addr_array[IPV4_ADDR_SIZE];
#undef s_addr
        uint32_t s_addr;
    };
};

// socket通用地址定义
struct x_sockaddr
{
    uint8_t sin_len;
    uint8_t sin_family;
    uint8_t sa_data[14];
};

// IPv4协议sock地址定义
struct x_sockaddr_in
{
    uint8_t sin_len;
    uint8_t sin_family;
    uint16_t sin_port;
    struct x_in_addr sin_addr;
    char sin_zero[8];
};

int x_socket(int family, int type, int protocol);                                                                          // socket接口的定义
ssize_t x_sendto(int socket, const void *buf, size_t len, int flags, const struct x_sockaddr *dest, x_socklen_t dest_len); // socket数据发送接口
ssize_t x_recvfrom(int socket, void *buf, size_t len, int flags, const struct x_sockaddr *src, x_socklen_t *src_len);      // sokcket数据接收接口
int x_setsockopt(int socket, int level, int optname, const char *optval, int len);                                         // 设置socket套接字的选项值
#endif                                                                                                                     // !SOCKET_H
